home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcr / pcr4_4.lha / DIST / package / ldunhide.c next >
C/C++ Source or Header  |  1989-04-03  |  3KB  |  130 lines

  1. /* begincopyright
  2.   Copyright (c) 1988 Xerox Corporation. All rights reserved.
  3.   Use and copying of this software and preparation of derivative works based
  4.   upon this software are permitted. Any distribution of this software or
  5.   derivative works must comply with all applicable United States export
  6.   control laws. This software is made available AS IS, and Xerox Corporation
  7.   makes no warranty about the software, its performance or its conformity to
  8.   any specification. Any person obtaining a copy of this software is requested
  9.   to send their name and post office or electronic mail address to:
  10.     PCR Coordinator
  11.     Xerox PARC
  12.     3333 Coyote Hill Rd.
  13.     Palo Alto, CA
  14.   endcopyright */
  15. /*
  16.  *  Copyright Xerox, Inc., 1988.
  17.  *     Permission to recopy for non-commercial purposes is granted, 
  18.  *     provided this copyright remains intact.
  19.  */
  20.  
  21. #include <sys/types.h>
  22. #include <sys/file.h>
  23. #include <sys/stat.h>
  24. #include <stdio.h>
  25. #include <a.out.h>
  26.  
  27. #define STR(x) ((char *)((x)->n_un.n_strx + (long)stringtab))
  28.  
  29. int quiet = 0;
  30. struct exec header;
  31. int num_syms;
  32. struct nlist *symtab, *find_sym();
  33. char *stringtab;
  34.  
  35. main(argc, argv)
  36. char **argv;
  37. {
  38.   int i;
  39.   char *filename;
  40.   FILE *file;
  41.   int str_len;
  42.   int file_length;
  43.   struct stat the_stat_buffer;
  44.   struct nlist *sym;
  45.  
  46.   /* check args */
  47.   if (argc < 3) {
  48.     printf("%s [-q] filename list-of-syms-to-unhide.\n\twhere -q means don't complain about missing or already unhidden symbols.\n", argv[0]);
  49.     exit(1);
  50.   }
  51.  
  52.   /* check for arguments */
  53.   if (strcmp(argv[1], "-q") == 0) {
  54.     quiet = 1;
  55.     argv += 1;
  56.     argc -= 1;
  57.   };
  58.   
  59.   /* try to get file */
  60.   filename = argv[1];
  61.   if ((file = fopen(filename, "r+")) == NULL) {
  62.     printf("ldhide: Cannot open file '%s'.\n", filename);
  63.     exit(1);
  64.   }
  65.  
  66.   /* get basic information about file */
  67.   fstat(fileno(file), &the_stat_buffer);
  68.   file_length = the_stat_buffer.st_size;
  69.   fread(&header, sizeof(struct exec), 1, file);
  70.  
  71.   /* get symbol table */
  72.   num_syms = header.a_syms / sizeof(struct nlist);
  73.   if (num_syms <= 0) {
  74.     printf("ldhide: No symbol table.\n");
  75.     exit(1);
  76.   }
  77.   symtab = (struct nlist *)calloc(num_syms, sizeof(struct nlist));
  78.   fseek(file, N_SYMOFF(header), 0);
  79.   fread(symtab, sizeof(struct nlist), num_syms, file);
  80.   
  81.   /* get string table for symbol table */
  82.   str_len = file_length - N_STROFF(header);
  83.   if (str_len <= 0) {
  84.     printf("ldhide: No string table.\n");
  85.     exit(1);
  86.   }
  87.   stringtab = (char *)calloc(str_len, 1);
  88.   fseek(file, N_STROFF(header), 0);
  89.   fread(stringtab, 1, str_len, file);
  90.  
  91.   /* look for symbols to hide, turn on the EXT bit */
  92.   for (i = 2; i < argc; i++) {
  93.     if ((sym = find_sym(argv[i]))) {
  94.       if ((sym->n_type & N_EXT)) {
  95.     if (!quiet) {
  96.       printf("ldhide: Symbol '%s' already visible.\n", argv[i]);
  97.     }
  98.       } else if ((sym->n_type & N_TYPE) == N_UNDF) {
  99.     printf("ldhide: Symbol '%s' is undefined: probably not a good idea to unhide it.\n", argv[i]);
  100.     } else {
  101.     sym->n_type |= N_EXT;
  102.       }
  103.     } else {
  104.       if (!quiet) {
  105.     printf("ldhide: Symbol '%s' not found.\n", argv[i]);
  106.       }
  107.     }
  108.   }
  109.  
  110.   /* write out new symtab */
  111.   fseek(file, N_SYMOFF(header), 0);
  112.   fwrite(symtab, sizeof(struct nlist), num_syms, file);
  113.   exit(0);
  114. }
  115.  
  116. /*
  117.  * proc to look for a symbol
  118.  */
  119. struct nlist *
  120. find_sym(s)
  121. char *s;
  122. {
  123.   int i;
  124.   for (i=0; i < num_syms; i++) {
  125.     if (strcmp(STR(&symtab[i]), s) == 0)
  126.       return &symtab[i];
  127.   }
  128.   return 0;
  129. }
  130.